package com.jbm.jbmsupplier.zp.liaotian;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jbm.jbmsupplier.R;
import com.jbm.jbmsupplier.activity.AllActivityApp;
import com.jbm.jbmsupplier.utlis.ExitActivity;
import com.jbm.jbmsupplier.zp.IndexBar.widget.IndexBar;
import com.jbm.jbmsupplier.zp.adapters.Buddy_ListAdapter;
import com.jbm.jbmsupplier.zp.decoration.TitleItemDecoration;
import com.jbm.jbmsupplier.zp.entity.Buddy_ListData;
import com.jbm.jbmsupplier.zp.presenters.TestDuiActivitys;
import com.jbm.jbmsupplier.zp.views.TestDuiViews;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;


/**
 * Created by donar_work on 2017/3/8.
 */

public class TestDuiHuaActivity extends AllActivityApp implements TestDuiViews {

    @InjectView(R.id.teleft)
    TextView teleft;
    @InjectView(R.id.tetitle)
    TextView tetitle;
    @InjectView(R.id.toolbar_image)
    ImageView toolbarImage;
    private RecyclerView lv_Buddy = null;
    private RecyclerView.Adapter buddy_listAdapter = null;
    private LinearLayoutManager mManager;
    /**
     * 右侧边栏导航区域
     */
    private IndexBar mIndexBar;
    /**
     * 显示指示器DialogText
     */
    private TextView mTvSideBarHint;
    private TestDuiActivitys testDuiActivitys;

    @Override
    public int setContentView() {
        return R.layout.text_duihua;
    }

    @Override
    public void initIalizeView() {
     ButterKnife.inject(this);
        ExitActivity.getInstance().addActivity(this);
        lv_Buddy = (RecyclerView) findViewById(R.id.Lv_Buddy);
        lv_Buddy.setLayoutManager(mManager = new LinearLayoutManager(this));
        mIndexBar = (IndexBar) findViewById(R.id.indexBar);//IndexBar
        mTvSideBarHint = (TextView) findViewById(R.id.tvSideBarHint);//HintTextView
        testDuiActivitys = new TestDuiActivitys(this, this);
        testDuiActivitys.InitData();
        tetitle.setText("好友");
        Picasso.with(this).load(R.mipmap.tianjiashigongdui).into(toolbarImage);
        toolbarImage.setVisibility(View.VISIBLE);
    }

    @Override
    public void setUpdata() {
        if (buddy_listAdapter != null) {
            buddy_listAdapter.notifyDataSetChanged();
        }
    }

    private TitleItemDecoration mDecoration;

    @Override
    public void getDataAda(ArrayList<Buddy_ListData> Buddy_ListData, int login, int tag) {
        if (tag == 1) {
            buddy_listAdapter = new Buddy_ListAdapter(getBaseContext(), Buddy_ListData, login);
            lv_Buddy.setAdapter(buddy_listAdapter);
            buddy_listAdapter.notifyDataSetChanged();
//            lv_Buddy.addItemDecoration(mDecoration = new TitleItemDecoration(getBaseContext(), Buddy_ListData));
            //如果add两个，那么按照先后顺序，依次渲染。
//            lv_Buddy.addItemDecoration(new DividerItemDecoration(getApplication(), DividerItemDecoration.VERTICAL_LIST));
        } else if (tag == 2) {
            buddy_listAdapter = new Buddy_ListAdapter(getApplication(), Buddy_ListData, login);
            lv_Buddy.setAdapter(buddy_listAdapter);
            buddy_listAdapter.notifyDataSetChanged();
            if (Buddy_ListData.size() == 0) {
                lv_Buddy.removeItemDecoration(mDecoration);
                return;
            }
            lv_Buddy.removeItemDecoration(mDecoration);
            mDecoration = new TitleItemDecoration(TestDuiHuaActivity.this, Buddy_ListData);
            lv_Buddy.addItemDecoration(mDecoration);
        }
//        mIndexBar.setmPressedShowTextView(mTvSideBarHint)//设置HintTextView
//                .setNeedRealIndex(false)//是否设置需要真实的索引
//                .setmLayoutManager(mManager)//设置RecyclerView的LayoutManager
//                .setmSourceDatas(Buddy_ListData);//设置数据源
    }

    @Override
    public void setOffUpdata() {
        lv_Buddy.removeAllViews();
        buddy_listAdapter.notifyDataSetChanged();
    }

    long che = 0;

    @Override
    public void onBackPressed() {
        finish();
//        if (System.currentTimeMillis() - che > 2000) {
//            Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
//            che = System.currentTimeMillis();
//        } else {
//            ExitActivity.getInstance().exitActivity();
        testDuiActivitys.onBackPressed();
//        }
    }



    @OnClick({R.id.teleft, R.id.toolbar_image})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.teleft:
                finish();
                break;
            case R.id.toolbar_image:
                startActivity(new Intent(TestDuiHuaActivity.this, MyAddContact.class));//添加好友
                break;
        }
    }
}